package cn.bavelee.pokeinstaller;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class BackgroundInstallActivity extends Activity {
    private boolean isContentUri = false;
    private Uri uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final String apkPath = getSourceApkPath();
        if (apkPath != null) {
            showToast(getString(R.string.start_install, apkPath));
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String realApkPath = apkPath;
                    File tempFile = new File(getExternalCacheDir(), System.currentTimeMillis() + ".apk");
                    if (isContentUri) {
                        try {
                            PokeInstaller.writeFile(getContentResolver().openInputStream(uri), tempFile);
                            realApkPath = tempFile.getPath();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (ShellUtils.execWithRoot("pm install -r --user 0 \"" + realApkPath + "\"") == 0) {
                        showToast(getString(R.string.apk_installed, getApkName(realApkPath)));
                    } else {
                        showToast(getString(R.string.install_failed, getApkName(realApkPath)));
                    }
                    if (isContentUri) tempFile.delete();
                    finish();
                }
            }).start();
        } else {
            showToast(getString(R.string.unable_to_install_apk));
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        finish();
    }

    public String getApkName(String apkSourcePath) {
        if (apkSourcePath == null) return null;
        PackageManager pm = getPackageManager();
        PackageInfo pkgInfo = pm.getPackageArchiveInfo(apkSourcePath, PackageManager.GET_ACTIVITIES);
        if (pkgInfo != null) {
            pkgInfo.applicationInfo.sourceDir = apkSourcePath;
            pkgInfo.applicationInfo.publicSourceDir = apkSourcePath;
            return pm.getApplicationLabel(pkgInfo.applicationInfo).toString();
        }
        return "";
    }

    private void showToast(final String text) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(BackgroundInstallActivity.this, text, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private String getSourceApkPath() {
        Intent intent = getIntent();
        Uri uri = intent.getData();
        if (uri != null) {
            this.uri = uri;
            if ("content".equalsIgnoreCase(uri.getScheme())) {
                isContentUri = true;
            }
            return uri.getPath();
        }
        return null;
    }
}
